package com.altekis.kanbankaze.client;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.VetoDragException;
import com.allen_sauer.gwt.dnd.client.drop.SimpleDropController;

/**
 * Controls the dropping of an avatar to a task
 * 
 * @author gabriprat
 * 
 */
public class TaskAvatarDropController extends SimpleDropController {

	private final TaskCardPresenter dropTargetPresenter;

	public TaskAvatarDropController(TaskCardView dropTarget, TaskCardPresenter dropTargetPresenter) {
		super(dropTarget);
		this.dropTargetPresenter = dropTargetPresenter;
	}

	/**
	 * Adds a style to allow the user know that the dragged object can be
	 * dropped here
	 */
	@Override
	public void onEnter(DragContext context) {
		this.getDropTarget().addStyleName("drag-over");
		super.onEnter(context);
	}

	/**
	 * Removes the style added by {@link #onEnter(DragContext)}
	 */
	@Override
	public void onLeave(DragContext context) {
		this.getDropTarget().removeStyleName("drag-over");
		super.onLeave(context);
	}

	/**
	 * Throws an exception to prevent the drop and make the dragged object
	 * return to its position and assigns the dragged avatar to the target task
	 */
	@Override
	public void onPreviewDrop(DragContext context) throws VetoDragException {
		
		String id = context.draggable.getElement().getId();
		dropTargetPresenter.getTask().setOwner(id);
		dropTargetPresenter.doSave();
		throw new VetoDragException(); // To make the draggable object return to
										// its original position
	}

}
